package com.zx.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.domain.Resource;
import com.zx.cloud.domain.enums.ResourceStatus;
import com.zx.cloud.model.TreeNode;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.model.response.Response;
import com.zx.cloud.service.LogService;
import com.zx.cloud.service.ResourceService;

import io.swagger.annotations.ApiOperation;


/**
 * 资源
 */
@RestController
@RequestMapping(value = ApiVersion.PROVIDER_V1_PREFIX+"/resource", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class ResourceController{

	private static final Logger logger=Logger.getLogger(ResourceController.class);
	
	@Autowired
	private ResourceService resourceService;
	@Autowired
	private LogService  logService; 
	
	@ApiOperation(value = "获取左侧菜单资源权限", notes = "获取左侧菜单资源权限")
	@RequestMapping(value = "/leftMenu", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<List<Resource>> leftMenu(@CurrentUserId Long userId){
		List<Resource> permissionrs = resourceService.findMenusByUserId(userId, null);
		logger.debug("*****************************获取资源集合，并处理**********************************");
		List<Resource> datas = new ArrayList<Resource>();
		if (permissionrs != null) {
			for (Resource permission : permissionrs) {
				if (permission.getParentId() == null) {
					for (final Resource p : permissionrs) {
						if (permission.getId().equals(p.getParentId())) {
							if(permission.getResources()!=null){
								permission.getResources().add(p);
							}else{
								List<Resource> resources=new ArrayList<Resource>();
								resources.add(p);
								permission.setResources(resources);
							}
						}
					}
					datas.add(permission);
				}
			}
		}
		return new ObjectDataResponse<List<Resource>>(datas);
	}
	
	@ApiOperation(value = "查询指定资源权限", notes = "查询指定资源权限")
	@RequestMapping(value = "/find", method = RequestMethod.POST)
	@ResponseBody
	public Response find(){
		Resource resource=new Resource();
		resource.setPageSize(Integer.MAX_VALUE);
		resource.setStatus(ResourceStatus.NOMAIL);
		List<Resource> datas=resourceService.findAllSource(resource);
		logger.debug("*****************************获取指定资源权限，并处理**********************************");
		if(datas==null){
			return new ObjectDataResponse<List<Resource>>(null);
		}
		return new ObjectDataResponse<List<Resource>>(datas);
	}
	
	@ApiOperation(value = "更新资源权限", notes = "更新资源权限")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Response update(@CurrentUserId Long userId,@RequestBody  Resource resource){
		int success=resourceService.update(resource);
		logger.debug("*****************************更新资源权限成功**********************************");
		logService.addLog(userId, "修改菜单资源", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
	@ApiOperation(value = "添加资源权限", notes = "添加资源权限")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Response add(@CurrentUserId Long userId,@RequestBody  Resource resource){
		int success=resourceService.insert(resource);
		logger.debug("*****************************添加资源权限成功**********************************");
		logService.addLog(userId, "添加菜单权限", 0);
		return new ObjectDataResponse<Integer>(success);
	}

	@ApiOperation(value = "删除资源权限", notes = "删除资源权限")
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Response delete(@CurrentUserId Long userId,@RequestBody  Resource resource){
		logger.debug("*****************************逻辑删除资源权限**********************************");
		logService.addLog(userId, "删除菜单", 0);
		resource.setStatus(ResourceStatus.DELETE);
		int success=resourceService.update(resource);
		logger.debug("*****************************删除资源权限成功**********************************");
		logService.addLog(userId, "删除菜单成功", 0);
		return new ObjectDataResponse<Integer>(success);
	}
	
}
